package com.sinosoft.dsp.platform.components.portal.input.elements;

import org.hibernate.Query;

import com.sinosoft.builder.templates.concept.TagElementParameterType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.platform.components.helper.TagElementHelper;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.Const;

public class SlideTagElememt implements ISinoTagElement {

	private static final Log log = Log.getLog(SlideTagElememt.class.getName());

	public static final String PARAMETER_MIN = "min";

	public static final String PARAMETER_MAX = "max";

	public static final String PARAMETER_STEP = "step";

	private Integer value;

	private int min = 0;

	private int max = 100;

	private int step = 1;

	@Override
	public boolean isEmpty() {
		return value == null;
	}

	@Override
	public void setParameter(Query query, String key) {
		if (!isEmpty()) {
			query.setParameter(key, value);
		}
	}

	@Override
	public void initValue(TagElementType type) throws SinoPlatformException {
		try {
			if (type != null) {
				TagElementParameterType minParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_MIN);
				TagElementParameterType maxParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_MAX);
				TagElementParameterType stepParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_STEP);

				if (!TagElementHelper.isEmptyTagElementParameter(minParam)) {
					this.min = Integer.valueOf(minParam.getValue());
				}

				if (!TagElementHelper.isEmptyTagElementParameter(maxParam)) {
					this.max = Integer.valueOf(maxParam.getValue());
				}

				if (!TagElementHelper.isEmptyTagElementParameter(stepParam)) {
					this.step = Integer.valueOf(stepParam.getValue());
				}
				if (!Const.isEmpty(type.getValue())) {
					this.value = Integer.valueOf(type.getValue());
				}
			}
		} catch (Exception e) {
			log.error("初始化滑动值出现错误。");
			throw new SinoPlatformException("初始化滑动值出现错误。");
		}
	}

	public void initValue(String... args) {
		if (args != null && args.length > 0) {
			this.value = Integer.valueOf(args[0]);

			if (args.length >= 2) {
				this.min = Integer.valueOf(args[1]);
			}
			if (args.length >= 3) {
				this.max = Integer.valueOf(args[2]);
			}
			if (args.length >= 4) {
				this.step = Integer.valueOf(args[3]);
			}
		}
	}

	public int getValue() {
		return value;
	}

	public void setValue(int value) {
		this.value = value;
	}

	public int getMin() {
		return min;
	}

	public void setMin(int min) {
		this.min = min;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

}
